package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.random.R;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.pojo.IndicatorEvaluationResult;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 二期 合成指数先行、滞后、一致基础指标双指标计算类
 * 2017-6-14
 */
public class HczsDivisionCalculateService extends IndicatorEvaluationCalculateService {


	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

			Double newVal = 0d;
			
			if (StringUtils.isNotBlank(indicator.getName()) && indicator.getName().contains("/")) {
				
				String[] names = indicator.getName().replace("合成指数", "").split("/");
				Double warningIndicatorVal =0d;
				if (names[0].equals(ProvinceIndicatorConstant.OCEAN_PRODUCTION_VALUE)) {
					Integer warningIndicatorId = getIndicatorEvaluationId(ProvinceIndicatorConstant.OCEAN_PRODUCTION_VALUE_HCZS, indicatorAccountVO.getAreaId());
					warningIndicatorVal = getWarningIndicatorVal(indicatorAccountVO, warningIndicatorId);//tbl_indicator_evaluation_res
					if (warningIndicatorVal==null || warningIndicatorVal.equals("")) {
						warningIndicatorVal=0d;
					}
				}else{
				//获取基础指标值-从录入指标或核算指标取
					warningIndicatorVal = getInputOrAccountingIndicatorValue(indicatorAccountVO,
						indicatorAccountVO.getYear(), names[0]);// 【/左侧值】
					if (warningIndicatorVal==null || warningIndicatorVal.equals("")) {
						warningIndicatorVal=0d;
					}
				}
				//获取基础指标值-从录入指标或核算指标取
				Double oldVal = getInputOrAccountingIndicatorValue(indicatorAccountVO,
						indicatorAccountVO.getYear(), names[1]);//【/右侧值】
				if (oldVal==null || oldVal.equals("")) {
					oldVal=1d;
				}
				newVal = warningIndicatorVal/oldVal;
			}
			
		return newVal;
	}
	
	private Double getWarningIndicatorVal(IndicatorAccountVO indicatorAccountVO, int id) {
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), id);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}

}
